;(function($, window, document,undefined) {
    //在插件中使用myModal对象
    $.fn.atlas = function(options) {
        //创建myModal的实体
        var mymodal = new myModal(this, options);
        //调用其方法
        return mymodal;
    }
    //定义myModal的构造函数
    var myModal = function(ele, opt) {
        this.$element = ele,
            this.defaults = {
                'box_id': 'atlas',
                'width' : 940,
                'height' : 470,
                'img_src' : '',
                'curr' : 1,
                'pages' : 10,
                //上一张按钮回调，options=当前参数信息
                '_prevCallBack' : function(options){},
                //下一张按钮回调，options=当前参数信息
                '_nextCallBack' : function(options){}
            },
            this.options = $.extend({}, this.defaults, opt),
            this.picture_style = {
                //图片原始宽高
                'default_width' : 0,
                'default_height' : 0,
                'default_top' : 0,
                'default_left' : 0,
                //图片的横竖宽高
                //修改前
                'levels_width' : 0,
                'levels_height' : 0,
                'levels_top' : 0,
                'levels_left' : 0,
                //修改后
                'vertically_width' : 0,
                'vertically_height' : 0,
                'vertically_top' : 0,
                'vertically_left' : 0,
                //图片真实宽高
                'real_width' : 0,
                'real_height' : 0,
                'real_top' : 0,
                'real_left' : 0,
                //显示区域宽高
                'outline_width' : 0,
                'outline_height' : 0,
                //旋转角度
                'angle' : 0,
                //放大倍数
                'adjustment_ratio' : 1
            }
    }
    //定义myModal的方法
    myModal.prototype = {
        createDiv : function(){
            var that = this,
                element = this.$element,
                options = this.options,
                box = options.box_id,
                picture_style = this.picture_style,
                img_frame_div = $('<div class="atlas_img_frame_div"></div>'),
                img_frame =  $('<div class="atlas_img_frame"></div>'),
                img_div = $('<div id="'+box+'" class="atlas_img_div"></div>'),
                btns = $('<div class="images_btns"></div>'),
                prev = $('<a href="javascript:void(0);">上一张</a>'),
                next = $('<a href="javascript:void(0);">下一张</a>'),
                turn_left = $('<a href="javascript:void(0);" id="turn_left">左转</a>'),
                turn_right = $('<a href="javascript:void(0);" id="turn_right">右转</a>'),
                enlarge = $('<a href="javascript:void(0);" id="enlarge">放大</a>'),
                narrow = $('<a href="javascript:void(0);" id="narrow">缩小</a>');
            btns.append(prev,next,turn_left,turn_right,enlarge,narrow);

            img_frame.append(img_div);
            img_frame_div.append(img_frame);
            img_frame_div.append(btns);
            element.append(img_frame_div);

            img_frame_div.css({
                'width' : options.width-12,
                'height' : options.height
            });
            img_frame.css({
                'width' : options.width-12,
                'height' : options.height-74
            });

            //获得外围div的宽高
            picture_style.outline_width = img_frame.width();
            picture_style.outline_height = img_frame.height();

            //获得图片信息
            that.getImage();

            //载入图片div
            that.loadImages();

            if(options.curr==1){
                prev.addClass('no_more');
            }
            else{
                prev.removeClass('no_more');
            }

            if(options.curr==options.pages){
                next.addClass('no_more');
            }
            else{
                next.removeClass('no_more');
            }

            //附加滚动及托转事件
            that.mouseWheel(box);
            that.dragEvent(box);
            //上一张按钮
            prev.on('click',function(e){
                if(options._prevCallBack){
                    options._prevCallBack(options);
                    next.removeClass('no_more');
                    console.log(options.curr);
                    if(options.curr==1){
                        prev.addClass('no_more');
                    }
                    else{
                        prev.removeClass('no_more');
                    }
                    that.getImage();
                    that.loadImages();
                }
            });
            //下一张按钮
            next.on('click',function(e){
                if(options._nextCallBack){
                    options._nextCallBack(options);
                    prev.removeClass('no_more');
                    console.log(options.curr);
                    if(options.curr==options.pages){
                        next.addClass('no_more');
                    }
                    else{
                        next.removeClass('no_more');
                    }
                    that.getImage();
                    that.loadImages();
                }
            });
            //左转按钮
            turn_left.on('click',function(){
                picture_style.angle -= 90;
                if(Math.abs(picture_style.angle)==360){
                    picture_style.angle = 0;
                }
                that.clickFlip(img_div);
            });
            //右转按钮
            turn_right.on('click',function(){
                picture_style.angle += 90;
                if(Math.abs(picture_style.angle)==360){
                    picture_style.angle = 0;
                }
                that.clickFlip(img_div);
            });
            //放大按钮
            enlarge.on('click',function(e){
                picture_style.adjustment_ratio += 1;
                img_div.css({
                    'transform' : 'scale('+picture_style.adjustment_ratio+') rotate('+picture_style.angle+'deg)'
                });
            })
            //缩小按钮
            narrow.on('click',function(e){
                picture_style.adjustment_ratio -= 1;
                if(picture_style.adjustment_ratio<1){
                    picture_style.adjustment_ratio = 1;
                }
                that.marginJudgment();
                img_div.css({
                    'transform' : 'scale('+picture_style.adjustment_ratio+') rotate('+picture_style.angle+'deg)',
                    'top' : picture_style.real_top,
                    'left' : picture_style.real_left
                });
            })
        },
        //获得图片信息
        getImage : function(){
            var element = this.$element,
                options = this.options,
                picture_style = this.picture_style,
                d_img = $('<img class="default_img" src="'+options.img_src+'"/>');
            d_img.css({
                'height' :options.height-74
            });
            element.append(d_img);
            if(d_img.width()>options.width-12){
                d_img.width(options.width-12);
                d_img.css('height','auto');
            }
            //赋值图片各种状态的宽和高
            picture_style.default_width = d_img.width();
            picture_style.default_height = d_img.height();
            picture_style.levels_width = d_img.width();
            picture_style.levels_height = d_img.height();
            picture_style.real_width = d_img.width();
            picture_style.real_height = d_img.height();

            d_img.css({
                'transform' : 'rotate(90deg)',
                '-ms-transform' : 'rotate(90deg)',/* Internet Explorer */
                '-moz-transform' : 'rotate(90deg)',/* Firefox */
                '-webkit-transform' : 'rotate(90deg)',/* Safari 和 Chrome */
                '-o-transform' : 'rotate(90deg)',/* Opera */
                'width' : options.height-74,
                'height' : 'auto'
            });

            picture_style.vertically_width = d_img.width();
            picture_style.vertically_height = d_img.height();
            d_img.remove();

            //赋值图片各种状态的top和left
            picture_style.real_top = ((picture_style.outline_height - picture_style.default_height)/2);
            picture_style.real_left = (picture_style.outline_width - picture_style.default_width)/2;
            picture_style.default_top = ((picture_style.outline_height - picture_style.default_height)/2);
            picture_style.default_left = (picture_style.outline_width - picture_style.default_width)/2;
            picture_style.levels_top = ((picture_style.outline_height - picture_style.levels_height)/2);
            picture_style.levels_left = (picture_style.outline_width - picture_style.levels_width)/2;
            picture_style.vertically_top = (picture_style.outline_height - picture_style.vertically_height)/2;
            picture_style.vertically_left = (picture_style.outline_width - picture_style.vertically_width)/2;
        },
        //载入图片
        loadImages : function(){
            var options = this.options,
                picture_style = this.picture_style;
            picture_style.adjustment_ratio = 1;
            picture_style.angle = 0;
            $('#'+options.box_id).css({
                'width': picture_style.default_width,
                'height': picture_style.default_height,
                'position': 'absolute',
                'top': ((picture_style.outline_height - picture_style.default_height)/2),
                'left': (picture_style.outline_width - picture_style.default_width)/2,
                'background': 'url("'+options.img_src+'") no-repeat center',
                'background-size': '100% 100%',
                'transform' : 'scale('+picture_style.adjustment_ratio+') rotate('+picture_style.angle+'deg)'
            });
        },
        //翻转点击事件
        clickFlip : function(img_div){
            var that = this,
                options = this.options,
                box = options.box_id,
                picture_style = this.picture_style;
            if(Math.abs(picture_style.angle)==90||Math.abs(picture_style.angle)==270){
                picture_style.real_width = picture_style.default_width = picture_style.vertically_width;
                picture_style.real_height = picture_style.default_height = picture_style.vertically_height;
                picture_style.real_top = picture_style.default_top = picture_style.vertically_top;
                picture_style.real_left = picture_style.default_left = picture_style.vertically_left;
            }
            else{
                picture_style.real_width = picture_style.default_width = picture_style.levels_width;
                picture_style.real_height = picture_style.default_height = picture_style.levels_height;
                picture_style.real_top = picture_style.default_top = picture_style.levels_top;
                picture_style.real_left = picture_style.default_left = picture_style.levels_left;
            }
            img_div.css({
                'transform' : 'rotate('+picture_style.angle+'deg)',
                '-ms-transform' : 'rotate('+picture_style.angle+'deg)',/* Internet Explorer */
                '-moz-transform' : 'rotate('+picture_style.angle+'deg)',/* Firefox */
                '-webkit-transform' : 'rotate('+picture_style.angle+'deg)',/* Safari 和 Chrome */
                '-o-transform' : 'rotate('+picture_style.angle+'deg)',/* Opera */
                'width' : picture_style.default_width,
                'height' : picture_style.default_height,
                'top' : picture_style.default_top,
                'left' : picture_style.default_left
            });
            picture_style.adjustment_ratio = 1;
        },
        //鼠标滚轮事件
        mouseWheel : function(id){
            var that = this,
                picture_style = this.picture_style,
                options = this.options,
                box = options.box_id;
            $('#'+id).on("mousewheel DOMMouseScroll", function (e) {
                var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) ||  // chrome & ie
                    (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1));              // firefox
                ///对img按下鼠标滚路，阻止视窗滚动
                e = e || window.event;
                if (e.stopPropagation) e.stopPropagation();
                else e.cancelBubble = true;
                if (e.preventDefault) e.preventDefault();
                else e.returnValue = false;
                picture_style.adjustment_ratio += delta/5;
                if(picture_style.adjustment_ratio<1){
                    picture_style.adjustment_ratio = 1;
                }
                if (delta < 0) {
                    // 向下滚
                    if(picture_style.adjustment_ratio>1){
                        that.marginJudgment();
                        $(this).css({
                            'width' : picture_style.default_width,
                            'height' : picture_style.default_height,
                            'top' : picture_style.real_top,
                            'left' : picture_style.real_left
                        });
                    }
                    else{
                        picture_style.real_width = picture_style.default_width;
                        picture_style.real_height = picture_style.default_height;
                        picture_style.real_top = picture_style.default_top;
                        picture_style.real_left = picture_style.default_left;
                        $(this).css({
                            'width' : picture_style.default_width,
                            'height' : picture_style.default_height,
                            'top' : picture_style.real_top,
                            'left' : picture_style.real_left
                        });
                    }
                }
                $(this).css({
                    'transform' : 'scale('+picture_style.adjustment_ratio+') rotate('+picture_style.angle+'deg)'
                });
                picture_style.real_width = picture_style.default_width*picture_style.adjustment_ratio;
                picture_style.real_height = picture_style.default_height*picture_style.adjustment_ratio;
            })
        },
        //鼠标滚轮边距判断
        marginJudgment : function(){
            var picture_style = this.picture_style;
            if(Math.abs(picture_style.angle)==90||Math.abs(picture_style.angle)==270){
                //图片头部边距
                if(picture_style.real_top>(picture_style.default_top+((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                    picture_style.real_top = (picture_style.default_top+((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                }
                //图片居底时底部边距
                if(picture_style.real_top<(picture_style.default_top-((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                    picture_style.real_top=(picture_style.default_top-((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                }
                if(picture_style.real_left!=picture_style.default_left){
                    //图片右侧边距
                    if(picture_style.real_left>picture_style.default_left+(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2)){
                        picture_style.real_left=picture_style.default_left+(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2);
                    }
                    //图片左侧边距
                    else if(picture_style.real_left<picture_style.default_left-(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2)){
                        picture_style.real_left=picture_style.default_left-(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2);
                    }
                }
                else{
                    picture_style.real_left=picture_style.default_left;
                }
            }
            else{
                //图片头部边距
                if(picture_style.real_top>((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2){
                    picture_style.real_top = ((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2;
                }
                if(picture_style.real_top<-((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2){
                    picture_style.real_top = -((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2;
                }
                //图片居底时底部边距
                if(picture_style.real_top<-(picture_style.default_height*picture_style.adjustment_ratio)+picture_style.outline_height){
                    picture_style.real_top=-(picture_style.default_height*picture_style.adjustment_ratio)+picture_style.outline_height
                }
                //图片底部边距
                else if(picture_style.real_top<(picture_style.outline_height-(picture_style.default_height*picture_style.adjustment_ratio))/2){
                    picture_style.real_top = (picture_style.outline_height-(picture_style.default_height*picture_style.adjustment_ratio))/2;
                }
                if(picture_style.real_left!=picture_style.default_left){
                    //图片右侧边距
                    if(picture_style.real_left>picture_style.default_left+(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                        picture_style.real_left=picture_style.default_left+(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                    }
                    //图片左侧边距
                    else if(picture_style.real_left<picture_style.default_left-(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                        picture_style.real_left=picture_style.default_left-(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                    }
                }
                else{
                    picture_style.real_left=picture_style.default_left;
                }
            }
        },
        //移除鼠标滚轮事件
        removeMouseWheel : function(id){
            $("#"+id).off("mousewheel DOMMouseScroll");
        },
        //绑定拖拽事件
        dragEvent : function(id){
            var picture_style = this.picture_style,
                options = this.options,
                move,
                _x,_y,
                xx,yy;
            $("#"+id).css('cursor','move');
            $("#"+id).on('mousedown',function(e){
                move=true;
                _x=e.pageX-parseFloat(picture_style.real_left);
                _y=e.pageY-parseFloat(picture_style.real_top);
                xx=e.pageX-_x;//控件左上角到屏幕左上角的相对位置
                yy=e.pageY-_y;
            }).on('mousemove',function(e){
                if(move){
                    xx=e.pageX-_x;//控件左上角到屏幕左上角的相对位置
                    yy=e.pageY-_y;
                    console.log(yy);
                    console.log(picture_style);
                    if(Math.abs(picture_style.angle)==90||Math.abs(picture_style.angle)==270){
                        if(picture_style.real_width == picture_style.outline_height){
                            yy = picture_style.default_top;
                            xx = picture_style.default_left;
                        }
                        else{
                            if(yy>(picture_style.default_top+((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                                yy = (picture_style.default_top+((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                            }
                            else if(yy<(picture_style.default_top-((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                                yy = (picture_style.default_top-((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                            }
                            if(xx>picture_style.default_left+(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2)){
                                xx=picture_style.default_left+(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2);
                            }
                            else if(xx<picture_style.default_left-(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2)){
                                xx=picture_style.default_left-(((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.default_height)/2);
                            }
                        }
                    }
                    else{
                        if((picture_style.default_height*picture_style.adjustment_ratio) == picture_style.default_height){
                            yy = picture_style.default_top;
                            xx = picture_style.default_left;
                        }
                        else{
                            if(yy>((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.outline_height)/2){
                                yy = ((picture_style.default_height*picture_style.adjustment_ratio)-picture_style.outline_height)/2;
                            }
                            else if(yy<(picture_style.outline_height-(picture_style.default_height*picture_style.adjustment_ratio))/2){
                                yy = (picture_style.outline_height-(picture_style.default_height*picture_style.adjustment_ratio))/2;
                            }
                            if(xx>picture_style.default_left+(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                                xx=picture_style.default_left+(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                            }
                            else if(xx<picture_style.default_left-(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2)){
                                xx=picture_style.default_left-(((picture_style.default_width*picture_style.adjustment_ratio)-picture_style.default_width)/2);
                            }
                        }
                    }
                    $("#"+id).css({"top":yy,"left":xx});
                    picture_style.real_left = xx;
                    picture_style.real_top = yy;
                }
            }).on('mouseup',function(){
                    move=false;
                    picture_style.real_left = xx;
                    picture_style.real_top = yy;
                }).on('mouseout',function(){
                    move=false;
                });
        },
        //移除拖拽事件
        removeDragEvent : function(id){
            $("#"+id).off("mousedown");
            $("#"+id).off("mousemove");
            $("#"+id).off("mouseup");
            $("#"+id).off("mouseout");
        }
    }
})(jQuery, window, document);